<?php
namespace app\admin\controller;
use think\Controller;

Class Attr extends BaseAdmin
{

	//属性列表
    public function lst()
    {
    
        $typeId=input('type_id');
        if($typeId){
            $map['type_id']=['=',$typeId];
        }else{
            $map=1;
        }
        $AttrRes=db('tp_attr')->alias('a')->field('a.*,t.type_name')->join('tp_type t',"a.type_id = t.id")->where($map)->order('a.id DESC')->paginate(6);
        $this->assign([
            'AttrRes'=>$AttrRes,
            ]);
        
        return $this->fetch('list');
    }

    //属性添加
    public function add()
    {
    	$TypeRes=db('tp_type')->select();
        $this->assign('TypeRes',$TypeRes);

        if(request()->isPost()){
        	$data=input('post.');
        	

        	//验证
        	// $validate = new \app\admin\validate\Brand;
        	// if (!$validate->check($data)) {
        	// 	$this->error($validate->getError());
        	// }
        	
        	$add=db('tp_attr')->insert($data);
        	if($add){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }

    //属性编辑
    public function edit($id)
    {
    	$AttrRes=db('tp_attr')->where(array('id'=>$id))->find();
        $TypeRes=db('tp_type')->select();
    	$this->assign([
            'AttrRes'=>$AttrRes,
            'TypeRes'=>$TypeRes

        ]);

        
        

    	if(request()->isPost()){
        	$data=input('post.');
        	
        	$up=db('tp_attr')->where(array('id'=>$id))->update($data);
        	
        	if($up!==false){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();
    }

    //属性删除
    public function del($id)
    {
    	$del=db('tp_attr')->delete($id);
        
    	
        if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }



    public function AjaxGetAttr(){
        $type_id=input('type_id');
        $Attr=db('tp_attr')->where(array('type_id'=>$type_id))->select();
        return json($Attr);
    }


    

    
    


}